package macrochip.vison.com.gps.manager;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

/**
 * Created by DELL on 2017/11/11.
 */

public class PermissionManager {
    private Activity activity;
    private PermissionManager() {
        }
        private static class SingletonHolder {
            private static PermissionManager instance = new PermissionManager();
        }

        public static PermissionManager getInstance() {
            return SingletonHolder.instance;
        }

    public void CheckLocalPermission(int MY_LOCAL_PERMISSION_REQUEST_CODE ,Activity activity){
        this.activity = activity;
        boolean isAllGranted = checkPermissionAllGranted(
                new String[] {
                        Manifest.permission.ACCESS_COARSE_LOCATION
                }
        );
        /**
         * 第 2 步: 请求权限
         */
        // 一次请求多个权限, 如果其他有权限是已经授予的将会自动忽略掉
        ActivityCompat.requestPermissions(
                activity,
                new String[] {
                        Manifest.permission.ACCESS_COARSE_LOCATION
                },
                MY_LOCAL_PERMISSION_REQUEST_CODE
        );
    }

    public void CheckStoragePermission(int MY_STORAGE_PERMISSION_REQUEST_CODE ,Activity activity){
        this.activity = activity;
        boolean isAllGranted = checkPermissionAllGranted(
                new String[] {
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                }
        );
        /**
         * 第 2 步: 请求权限
         */
        // 一次请求多个权限, 如果其他有权限是已经授予的将会自动忽略掉
        ActivityCompat.requestPermissions(
                activity,
                new String[] {
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                },
                MY_STORAGE_PERMISSION_REQUEST_CODE
        );
    }

    /**
     * 检查是否拥有指定的所有权限
     */
    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                return false;
            }
        }
        return true;
    }

    public void openAppDetails() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage("Xendrone需要访问 “手机位置信息”，请到 “应用信息 -> 权限” 中授予！");
        builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + activity.getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                activity.startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    public void openStorageDetails(){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage("Xendrone需要访问 “手机储存信息”，请到 “应用信息 -> 权限” 中授予！");
        builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + activity.getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                activity.startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }
}
